//
//  LCButton.m
//  04 -自定制tabBar
//
//  Created by 千锋 on 16/5/7.
//  Copyright (c) 2016年 千锋. All rights reserved.
//

#import "LCButton.h"

@interface LCButton(){
  
    
    id _target;
    SEL _action;
}
@property(nonatomic,strong)UIFont *titleFont;

@property(nonatomic,strong)UIColor *titleColor;

@property(nonatomic,strong)UILabel *titleLabel;

@property(nonatomic,strong)UIImageView *imageView;

@end

@implementation LCButton
#pragma mark - 懒加载(单纯的创建子视图)
- (UIFont *)titleFont{
    if (!_titleFont) {
        _titleFont = [UIFont systemFontOfSize:12];
    }
    return _titleFont;
}
- (UIColor *)titleColor{
    if (!_titleColor) {
        _titleColor = [UIColor blackColor];
    }
    return _titleColor;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}
- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [UIImageView new];
        [_imageView setContentMode:UIViewContentModeCenter];
        [self addSubview:_imageView];
    }
    return _imageView;
}

#pragma mark - 给子视图的属性赋值

//外部如果既给文字赋值又给图片赋值,那么图片在上,文字在下;
//如果只给文字赋值,将文字显示在中间;
//如果只给图片赋值,将图片显示在中间;

//如果给文字赋值,就创建这个显示文字的label
- (void)setTitle:(NSString *)title{
    _title = title;
    //=======实例化label===========
    self.titleLabel.text = title;
    
}
- (void)setImage:(UIImage *)image{
    _image = image;
    self.imageView.image = image;
}
#pragma mark - 设置子视图的frame
- (void)layoutSubviews{
    [super layoutSubviews];
    
    //上图下文zi(3：1)
    
    //获取该视图的宽高
    CGFloat  btnW = self.frame.size.width;
    CGFloat  btnH = self.frame.size.height;
    CGFloat imageH;
    CGFloat titleH;
    if (_titleLabel) {//根据文字设置图片
        imageH = btnH * 3 / 4;
    }else{
        imageH = btnH;
    }
    if(_imageView){ //根据图片设置图片高度
        titleH = btnH * 3 / 4;
    }else{
        titleH = btnH;
    }
    //图片
    CGFloat imageX = 0;
    CGFloat imageY = 0;
    _imageView.frame = CGRectMake(imageX, imageY, btnW, imageH);
        //文字
    CGFloat titleX = 0;
    CGFloat titleY = _imageView.frame.size.height -12;
    _titleLabel.frame = CGRectMake(titleX, titleY, btnW, titleH);
    // =====================设置其他属性==================
    _titleLabel.font = _titleFont;
    _titleLabel.textColor = _titleColor;
}
#pragma mark - 改变选中状态
- (void)setIsSelected:(BOOL)isSelected{
    _isSelected = isSelected;
    //根据状态重新设置imageView的图片
    if (self.isSelected) {
        _imageView.image = _selectedImage;
        _titleLabel.textColor = [UIColor colorWithRed:66/255.0f green:175/255.0f blue:250/255.0f alpha:1];
     
    }else{
        _imageView.image = _image;
       _titleLabel.textColor = self.titleColor;
        
    }

}

#pragma mark - 设置文字颜色和字体
- (void)setTitleFont:(UIFont *)font color:(UIColor *)color{
    
    //保存字体和颜色
    _titleFont = font;
    _titleColor = color;
}
#pragma mark - 按钮点击事件
- (void)addTarget:(id)target action:(SEL)action{

    _action = action;
    _target = target;
}
#pragma mark - 按下的时候,对象响应消息
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    if ([_target respondsToSelector:_action]) {
        [_target performSelector:_action withObject:self];
    }else{
        NSLog(@"没有实现按钮点击的事件");
    }
}
@end
